<template>
  <div id="main" style="width: 100%; height: 100%"></div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../../dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      const animationDuration = 1500

      const barName = ['区域A', '区域A', '区域A', '区域A', '区域A']
      const rzData= [35, 40, 65, 30, 45]
      const lzData= [35, 45, 30, 38, 25]

      this.chart.setOption ({
        title: {
          text: '对比分析',
          left: 'left'
        },
        color: ['#2ec7c9', '#ED7D31', '#5ab1ef'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          left: 'center',
          top: '0',
        },
        grid: [
          {
            show: false,
            left: '3%',
            top: '30',
            bottom: '0%',
            containLabel: true,
            width: '38%'
          },
          {
            show: false,
            left: '51.5%',
            top: '30',
            bottom: '6%',
            width: '0%'
          },
          {
            show: false,
            right: '3%',
            top: '30',
            bottom: '0%',
            containLabel: true,
            width: '38%'
          }
        ],
        xAxis: [
          {
            // name: '人数',
            type: 'value',
            max: 100,
            inverse: true, // 强制设置坐标轴分割间隔
            // axisLine: {
            //   // 坐标轴轴线相关设置
            //   show: false, // 是否显示坐标轴轴线
            //   onZero: true, // X 轴或者 Y 轴的轴线是否在另一个轴的 0 刻度上
            //   lineStyle: {
            //     color: '#ECEDF0'
            //   }
            // },
            // axisTick: {
            //   show: false
            // },
            // position: 'bottom',
            axisLabel: {
              show: false,
              color: '#c6d3d7',
              fontSize: 12,
              fontFamily: 'DINPro-Regular'
            },
            splitLine: {
              show: false
            }
          },
          {
            gridIndex: 1,
            show: false
          },
          {
            gridIndex: 2,
            // axisLine: {
            //   show: false, // 是否显示坐标轴轴线
            //   onZero: true, // X 轴或者 Y 轴的轴线是否在另一个轴的 0 刻度上
            //   lineStyle: {
            //     color: '#ECEDF0'
            //   }
            // },
            type: 'value',
            max: 100,
            axisTick: {
              show: true
            },
            // position: 'bottom',
            axisLabel: {
              show: false,
              color: '#c6d3d7',
              fontSize: 12,
              fontFamily: 'DINPro-Regular'
            },
            splitLine: {
              show: false
            }
          }
        ],
        yAxis: [
          {
            type: 'category',
            inverse: true,
            position: 'right',
            axisLine: {
              show: true,
              lineStyle: {
                color: '#606266'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            data: barName
          },
          {
            gridIndex: 1,
            type: 'category',
            inverse: true,
            position: 'left',
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#606266',
                fontSize: 12,
                fontFamily: 'pmzd_title'
              },
              align: 'center'
            },
            data: barName.map(function(value) {
              return {
                value: value,
                textStyle: {
                  align: 'center'
                }
              }
            })
          },
          {
            gridIndex: 2,
            type: 'category',
            inverse: true,
            position: 'left',
            axisLine: {
              show: true,
              lineStyle: {
                color: '#606266'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            data: barName
          }
        ],
        series: [
          {
            name: '漏损量',
            type: 'bar',
            itemStyle: {
              normal: {
                barBorderRadius: [50,0,0,50],
                label: {
                  show: true, // 开启显示
                  position: 'left', // 在上方显示
                  textStyle: {
                    // 数值样式
                    // color: colors[0].borderColor,
                    color: '#606266',
                    fontSize: 12
                  }
                },
                borderWidth: 1
              }
            },
            data: rzData,
            showBackground: true,
            backgroundStyle: {

              color: 'rgba(180, 180, 180, 0.2)'
            },
            animationDuration
          },
          {
            name: '漏损率',
            type: 'bar',
            xAxisIndex: 2,
            yAxisIndex: 2,
            itemStyle: {
              normal: {
                barBorderRadius: [0,50,50,0],
                label: {
                  show: true, // 开启显示
                  position: 'right', // 在上方显示
                  textStyle: {
                    // 数值样式
                    color: '#606266',
                    fontSize: 12
                  },
                },
                borderWidth: 1
              }
            },
            data: lzData,
            showBackground: true,
            backgroundStyle: {

              color: 'rgba(180, 180, 180, 0.2)'
            },
            animationDuration
          }
        ]
      })
    }
  }
}
</script>

